package com.chenglin.mapdemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chenglin.mapdemo.R;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 搜索顶部tips 适配器
 */
public class SearchTipsAdapter extends RecyclerView.Adapter<SearchTipsAdapter.MyHolder> {

    private Context context;
    private List<String> list;
    private TipsCallBack callBack;

    public SearchTipsAdapter(Context context, List<String> list,TipsCallBack callBack) {
        this.context = context;
        this.list = list;
        this.callBack = callBack;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.dialog_search_item_tips, parent, false);
        return new MyHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        holder.tvTips.setText(list.get(position));
        // 虚拟设置样式和字体颜色 根据自己需求决定
        if (position % 2 == 0){
            holder.tvTips.setBackground(context.getResources().getDrawable(R.drawable.bg_search_tip_blue));
            holder.tvTips.setTextColor(context.getResources().getColor(R.color.colorBlue));
        }else {
            holder.tvTips.setBackground(context.getResources().getDrawable(R.drawable.bg_search_tip_gray));
            holder.tvTips.setTextColor(context.getResources().getColor(R.color.colorBlack));
        }

//        设置最后一列距离屏幕的编剧
//        setLastItemRightMargin(holder, position);

        holder.tvTips.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.onClickCallBack(position);
            }
        });

    }


    /**
     *
     * 看需求 如果 效果是搜索框的分类列表 左右不留边添加这个方法  并且去掉布局文件中 的recyclerVie
     * marginRight="20dp"
    *
     * 设置最后一列距离屏幕的边距
     * @param holder
     * @param position
     */
    private void setLastItemRightMargin(@NonNull MyHolder holder, int position) {
        if (list.size() % 2 == 0){
            if (position == list.size() -1 || position == list.size() -2){
                ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams)holder.tvTips.getLayoutParams();
                layoutParams.setMarginEnd(50);
                holder.tvTips.requestLayout();
            }else {

            }
        }else {
            if (position == list.size() -1 ){
                ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams)holder.tvTips.getLayoutParams();
                layoutParams.setMarginEnd(50);
                holder.tvTips.requestLayout();
            }else {

            }
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }



    public class MyHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_tips)
        TextView tvTips;
        public MyHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    public interface TipsCallBack{
        void onClickCallBack(int position);
    }
}
